<?php
class yc_http extends spController
{

	//主检测模块http检测
	function __construct(){ 
		parent::__construct(); 
	}
	function index(){ // 这里是首页
		$this->display("produce/main_index.html");
	}
	
	function page(){ // 其他内容
		$this->start_detection();
		$this->display("produce/main_page.html");
	}
	function start_detection()
	{
		//这里是开启检测功能
		//$this->detection();
		$time = $this->spArgs('time');
		$this->moniter($time);
	}	
	/**
	 * 检测
	 * Enter description here ...
	 * @param unknown_type $interval
	 */
	public function detection($interval)
	{
		if ($interval==""){$interval=60;}
		$site = spClass("site");
		$where['active'] = 1;
		$tttc=60;
		$where['intervals'] = $interval / $tttc;
		$time_id = $where['intervals'];
		$runtime = spClass("runtime");
		$runtime->u_time($time_id);
		unset($runtime);
		$b = $site->findAll($where);
		foreach ($b as $i)
		{
			$station=explode("-", $i["station"]);
			dump($station);
			foreach ($station as $t => $s){
				$sstn=spClass("station");
				$condition["stationid"]=$s;
				$s_d=$sstn->find($condition);
				$s_d["domain"];
				dump($s_d);
				
				$path="produce.php?";
				$dat=array(
					siteid=>$i["siteid"],
					domain=>$i["domain"],
				);
				foreach($dat as $k => $v){
   		 			$path .= $k . "=" . $v . "&";
				};
				dump($path);
				//$this->ffp($path, $s_d["domain"]);
			
			
			
			
			}
			
/*
			$url = $i[domain];
			$s_date = date("Y-m-d H:i:s");
			$status=$this->http_p($url);
			unset($url);
			$n_date = date("Y-m-d H:i:s");
			$sb=strtotime($n_date)-strtotime($s_date);

			import("detection.php");
			$get_ip = new detection();
			$ip=$get_ip->g_ip($i[domain]);
			unset($get_ip);

			$Model = spClass('record');
			$conditions = array
			(
				'siteid'=>$i[siteid],
			);
			$ns=$Model->etr($conditions);
			if($ns['0'][status_code]<>$status)
			{
				$data = array(
					'status_code'=>$status,
					'userid'=>$i[userid],
					'siteid'=>$i[siteid],
					'type'=>'',
					'size'=>'0',
					'taketime'=>$sb,
					'addtime'=>strtotime(date("Y-m-d H:i:s")),
					'site_ip'=>$ip,				
				);

				$Model->create($data);
					
				$user = spClass('user');
				$conditions = array(
					'userid'=> $i[userid]
				);
				$u = $user->findall($conditions);
				$as=array(
					'siteid'=>$i[siteid], 'domain'=>$i[domain],	'username'=>$u[0][username],
					'mail'=>$u[0][mail], 'userid'=>$i[userid], 	'status'=>$status
				);
				$Model->alarm($as);
				
				unset($Model);
				$ssdate= array(
					'status_code' => $status,
					'lasttime' => $data['addtime'],
					'alarmn' => $i['alarmn']+1,
				);
				$wwe['siteid']=$i[siteid];
				$site->update($wwe,$ssdate);
				unset($data);
			}
*/
		}
		unset($site);
		unset($b); 

	}

	/**
	 +----------------------------------------------------------
	 * 网站状态测试
	 +----------------------------------------------------------
	 */
	public function http_p($url,$method,$postDate)
	{
		 
		$method ='GET';
		$postDate = '';
		import("WebRequest.php");
		$ht_tp = new WebRequest($url,$method,$postDate);

		if (!$c=$ht_tp->getResponse())
		{
			return 'could not open';
		}
		else
		{
			return $c->status;
		}
	}

	/**
	 +----------------------------------------------------------
	 * 循环
	 +----------------------------------------------------------
	 */

	private function moniter($time)
	{
		ignore_user_abort(); 
		set_time_limit(0); 
		$interval= $time;
		date_default_timezone_set("Asia/Shanghai"); 
		do
		{
			$this->detection($interval);
			sleep($interval);

		}while(true);
	}
	/**
	 * ////
	 * Enter description here ...
	 * @param unknown_type $siteid
	 */
	function alarm($siteid)
	{
		$record = spClass('record');
		$n = $record->etc($siteid);
		if($n[0][status_code]=200){
			if($n[1][status_code]=200){
				break;
			}else{
				$time = $n[0][addtime]-$n[1][addtime];
			}
		}
		else{
			if ($n[1][status_code]=200){
				$time = $n[0][addtime]-$n[1][addtime];
			}
			else{
				break;
			}
		}
	}
	/**
	 * 模拟访问
	 */
	function ffp($path) 
	{
		$system = spClass("system");
		
		$site = $system->findAll();
		$domain = $site[0]['domain'];
		$fp = fsockopen ( "$domain", 80, $errno, $errstr, 30 );
		if (! $fp){
			echo "$errstr ($errno)<br />\n";
		} else{
			$header = "GET /$path HTTP/1.1\r\n";
			$header .= "Host:  $domain\r\n";
			$header .= "Connection: Close\r\n\r\n";
			fwrite ( $fp, $header );
			fclose ( $fp );
			return true;
		}
	}
	/**
	 * 远程访问
	 */
	function y_f()
	{
		$data=spArgs();
		$s_date = date("Y-m-d H:i:s");
		$status=$this->http_p($data[domain]);
		$n_date = date("Y-m-d H:i:s");
		$sb=strtotime($n_date)-strtotime($s_date);

		import("detection.php");
		$get_ip = new detection();
		$ip=$get_ip->g_ip($data[domain]);
		unset($get_ip);
		
		$status;$sb;$ip;
	}
	/**
	 * 获取检测数据
	 * Enter description here ...
	 */
	function huoqu()
	{
		$a=$this->spArgs();
		$status=$a["status"];
		$site=spClass("site");
		$where["siteid"]=$a["siteid"];
		$i = $site->find($where);
		$Model = spClass('record');
		$conditions = array(
				'siteid'=>$i[siteid],
		);
		$ns=$Model->etr($conditions);
		if($ns['0'][status_code]<>$status){
			$data = array(
					'status_code'=>$status,
					'userid'=>$i[userid],
					'siteid'=>$i[siteid],
					'type'=>'',
					'size'=>'0',
					'taketime'=>$a["time"],
					'addtime'=>strtotime(date("Y-m-d H:i:s")),
					'site_ip'=>$ip,
					'stationid'=>$a["stationid"],
			);
			$Model->create($data);
				
			$user = spClass('user');
			$conditions = array(
					'userid'=> $i[userid]
			);
			$u = $user->findall($conditions);
			$as=array(
					'siteid'=>$i[siteid], 'domain'=>$i[domain],	'username'=>$u[0][username],
					'mail'=>$u[0][mail], 'userid'=>$i[userid], 	'status'=>$status
			);
			$Model->alarm($as);

			unset($Model);
			$ssdate= array(
					'status_code' => $status,
					'lasttime' => $data['addtime'],
					'alarmn' => $i['alarmn']+1,
			);
			$wwe['siteid']=$i[siteid];
			$site->update($wwe,$ssdate);
			unset($data);

		}
	}
	function paost(){
		$data=$this->spArgs();
		dump($data);
		//$qqw=$_POST;
		//dump($qqw);
	}
}